package com.example.demo.controllers;

import com.example.demo.common.utils.ToolsUtil;
import com.example.demo.entity.HW;
import com.example.demo.services.HWService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * @Author: Ding XiJie
 */
@RequestMapping("/hw")
@Controller
public class HWController {
    @Autowired
    private HWService hwService;

    /**
     * @Author: Ding XiJie
     * @Description: 添加货物
     */
    @RequestMapping("/save")
    @ResponseBody
    public Map<String, Object> save(HW hw){
        try {
            hw = hwService.save(hw);
            return ToolsUtil.returnMessage(0,"添加成功");
        }catch (Exception e){
            e.printStackTrace();
            return ToolsUtil.returnMessage(0,"添加失败");
        }
    }

    /**
     * @Author: Ding XiJie
     * @Description: 修改货物
     */
    @RequestMapping("/update")
    @ResponseBody
    public Map<String, Object> update(HW hw){
        try {
            hw = hwService.update(hw);
            return ToolsUtil.returnMessage(0,"修改成功");
        }catch (Exception e){
            e.printStackTrace();
            return ToolsUtil.returnMessage(0,"修改失败");
        }
    }

    /**
     * @Author: Ding XiJie
     * @Description: 删除货物
     */
    @RequestMapping("/delete")
    @ResponseBody
    public Map<String, Object> delete(Integer hwid){
        try {
            hwService.delete(hwid);
            return ToolsUtil.returnMessage(0,"删除成功");
        }catch (Exception e){
            e.printStackTrace();
            return ToolsUtil.returnMessage(0,"删除失败");
        }
    }

    /**
     * @Author: Ding XiJie
     * @Description: 查询所有快递/货物
     */
    @RequestMapping("/getAllHw")
    @ResponseBody
    public Map<String, Object> getAllKh(Model model){
        Integer count = 0;
        try {
            List<HW> khList = hwService.getAllHw();
            count = hwService.findCount();
            return ToolsUtil.returnTable(0,count,"查询成功",khList);
        }catch (Exception e){
            e.printStackTrace();
            return ToolsUtil.returnTable(0,count,"查询失败",null);
        }
    }

    /**
     * @Author: Ding XiJie
     * @Description: 跳转货物页面
     */
    @RequestMapping("/toHw")
    public String toHw(Model model){
        return "user_hw.html";
    }

    /**
     * @Author: Ding XiJie
     * @Description: 跳转快递列表页面
     */
    @RequestMapping("/toHwList")
    public String toHwList(Model model){
        return "kdgl_hw.html";
    }
}
